package de.slothsoft.wasbinich.swingclient.control;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import de.slothsoft.commons.swing.AbstractListTableModel;
import de.slothsoft.commons.swing.ITableCreator;
import de.slothsoft.commons.swing.JTableUtil;
import de.slothsoft.commons.swing.JToolBarManager;
import de.slothsoft.commons.swing.SlothsoftTableCellRenderer;
import de.slothsoft.commons.swing.TableCreatorUtil;
import de.slothsoft.wasbinich.core.FullPicture;
import de.slothsoft.wasbinich.core.database.Database;
import de.slothsoft.wasbinich.core.database.DatabaseException;
import de.slothsoft.wasbinich.swingclient.GlobalEventScopes;
import de.slothsoft.wasbinich.swingclient.WasBinIchSwingClient;
import de.slothsoft.wasbinich.swingclient.common.GlobalEvent;
import de.slothsoft.wasbinich.swingclient.common.GlobalEventHandler;
import de.slothsoft.wasbinich.swingclient.common.GlobalListener;
import de.slothsoft.wasbinich.swingclient.control.TitlePanel.ScreenSwitcher;
import de.slothsoft.wasbinich.swingclient.io.Dialogs;
import de.slothsoft.wasbinich.swingclient.io.ImageUtil;
import de.slothsoft.wasbinich.swingclient.res.Images;
import de.slothsoft.wasbinich.swingclient.res.Messages;
import de.slothsoft.wasbinich.swingclient.table.AbstractPictureTableModel;
import de.slothsoft.wasbinich.swingclient.table.DefaultPictureTableModel;

public class PicturesPanel extends ChildPanel implements ITableCreator<FullPicture>, GlobalListener {

    private static final long serialVersionUID = -562694750455822666L;

    private JTable table;
    private DefaultPictureTableModel model;
    private JButton createButton;
    private JButton editButton;
    private JButton deleteButton;

    public PicturesPanel(ScreenSwitcher screenSwitcher) {
	super(screenSwitcher);
	GlobalEventHandler.getGlobalInstance().register(GlobalEventScopes.LOG_IN, this);
	GlobalEventHandler.getGlobalInstance().register(GlobalEventScopes.LOG_OUT, this);
    }

    @Override
    protected Component createControls() {
	JPanel panel = new JPanel();
	this.table = TableCreatorUtil.buildTable(this, panel);
	this.model = (DefaultPictureTableModel) this.table.getModel();
	return panel;
    }

    @Override
    public void showPanel() {
	try {
	    this.model.refresh();
	} catch (DatabaseException e) {
	    WasBinIchSwingClient.handleException(e);
	}
    }

    @Override
    public AbstractListTableModel<FullPicture> createModel() {
	return new DefaultPictureTableModel();
    }

    @Override
    public int[] createColumnWidths() {
	return AbstractPictureTableModel.COLUMNS_WIDTH;
    }

    @Override
    public void performSelectionChanged() {
	boolean somethingSelected = JTableUtil.getSelectedElement(this.table, this.model) != null;
	this.editButton.setEnabled(somethingSelected);
	this.deleteButton.setEnabled(somethingSelected);
    }

    @Override
    public void performDoubleClick() {
	SwitchPanel newScreen = getScreenSwitcher().switchScreen(MainPanel.SCREEN_PICTURE);
	Long id = JTableUtil.<FullPicture> getSelectedElement(this.table, this.model).id;
	try {
	    ((PicturePanel) newScreen).setPicture(Database.getInstance().getManagerBean().getPicture(id));
	} catch (DatabaseException e) {
	    WasBinIchSwingClient.handleException(e);
	}
    }

    @Override
    protected void fillToolbar(JToolBarManager manager) {
	super.fillToolbar(manager);
	manager.addSeparator();
	this.createButton = manager.add(Images.get(Images.TOOLBAR_CREATE), new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		createPicture();
	    }
	});
	this.createButton.setEnabled(false);
	this.editButton = manager.add(Images.get(Images.TOOLBAR_EDIT), new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		performDoubleClick();
	    }
	});
	this.editButton.setEnabled(false);
	this.deleteButton = manager.add(Images.get(Images.TOOLBAR_DELETE), new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		deletePicture();
	    }
	});
	this.deleteButton.setEnabled(false);
    }

    protected void createPicture() {
	File file = Dialogs.selectImageFile(this);
	if (file != null) {
	    try {
		FullPicture picture = ImageUtil.createPictureFromFile(file);
		SwitchPanel newScreen = getScreenSwitcher().switchScreen(MainPanel.SCREEN_PICTURE);
		((PicturePanel) newScreen).setPicture(picture);
	    } catch (IOException e) {
		Logger.getGlobal().severe(e.getMessage());
	    }
	}
    }

    protected void deletePicture() {
	Long id = JTableUtil.<FullPicture> getSelectedElement(this.table, this.model).id;
	if (JOptionPane.showConfirmDialog(this, Messages.getString("Question.deletePicture.message"),
		Messages.getString("Question.deletePicture.title"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
	    try {
		Database.getInstance().getManagerBean().deletePicture(id.longValue());
		this.model.refresh();
	    } catch (DatabaseException e) {
		WasBinIchSwingClient.handleException(e);
	    }
	}
    }

    @Override
    public JComponent createFilterComponent() {
	return null;
    }

    @Override
    public JComponent createInformationComponent() {
	return null;
    }

    @Override
    public TableCellRenderer createRenderer() {
	return new SlothsoftTableCellRenderer();
    }

    @Override
    public void handleEvent(GlobalEvent event) {
	this.createButton.setEnabled(Database.getInstance().getLoginBean().isLoggedIn());
    }
}
